﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using TerminationCall.Editor.Logic;
using TerminationCall.Core;
using System.IO;
using System.Text.RegularExpressions;
using System.Reflection;

namespace TerminationCall.Editor.GUI
{
    public partial class TilesForm : Form
    {
        private int _lastSelectedTileObjectGroupIndex = 0;

        public EditorTool _editorTool;

        public TilesForm(EditorTool editorTool) {
            InitializeComponent();

            _editorTool = editorTool;

            _editorTool.LevelChanged += new EventHandler<EventArgs>(_editorTool_LevelChanged);
            _editorTool_LevelChanged(this, EventArgs.Empty);
        }

        void _editorTool_LevelChanged(object sender, EventArgs e) {

            cobTileSets.Items.Clear();

            for (int i = 0; i < _editorTool.Level.TileSets.Count; i++) {
                cobTileSets.Items.Add(i.ToString() + " - " + _editorTool.Level.TileSets[i].Name);
            }

            cobTileSets.SelectedIndex = _editorTool.Level.TileSets.Count - 1;

        }

        private void cobTileSets_SelectedIndexChanged(object sender, EventArgs e) {

            _editorTool.ChoosenTileSet = _editorTool.Level.TileSets[GetIndexFromSelection()];

            tileSetRender1.TileSet = _editorTool.ChoosenTileSet;
            tileSetRender1.Selection = new Rectangle(1, 1, 1, 1);

            tileSetGroupedAutoborderView1.TileSet = _editorTool.ChoosenTileSet;

            if (_editorTool.ChoosenTileSet == null) {
                propertiesToolStripMenuItem.Enabled = false;
                saveAsToolStripMenuItem.Enabled = false;
            } else {
                propertiesToolStripMenuItem.Enabled = true;
                saveAsToolStripMenuItem.Enabled = true;
            }
        }

        private void propertiesToolStripMenuItem_Click(object sender, EventArgs e) {

            TileSetPropertiesForm tileSetPropertiesForm = new TileSetPropertiesForm(_editorTool.ChoosenTileSet);
            tileSetPropertiesForm.ShowDialog();

            int index = GetIndexFromSelection();
            cobTileSets.Items.Remove(cobTileSets.SelectedItem);
            cobTileSets.SelectedIndex = cobTileSets.Items.Add(index.ToString() + " - " + _editorTool.ChoosenTileSet.Name);


        }

        private void tileSetRender1_TileSelectionChange(object sender, EventArgs e) {

            _editorTool.ChoosenTiles = tileSetRender1.Selection;

        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e) {

            if (ofdAddTileSet.ShowDialog() == DialogResult.OK && ofdAddTileSet.FileName != string.Empty) {
                if (File.Exists(ofdAddTileSet.FileName)) {
                    _editorTool.Level.TileSets.Add(new TileSet(ofdAddTileSet.FileName, 20, 20));

                    cobTileSets.SelectedIndex = cobTileSets.Items.Add((_editorTool.Level.TileSets.Count - 1).ToString() + " - " + _editorTool.Level.TileSets[_editorTool.Level.TileSets.Count - 1].Name);
                }
            }

        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e) {

            sfdSaveTileset.FileName = Path.GetFileNameWithoutExtension(_editorTool.ChoosenTileSet.Name);

            if (sfdSaveTileset.ShowDialog() == DialogResult.OK) {

                TileSetSaver tileSetSaver = new TileSetSaver();

                tileSetSaver.Save(sfdSaveTileset.FileName, _editorTool.ChoosenTileSet);
            }

        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e) {

            if (ofdOpenTileset.ShowDialog() == DialogResult.OK) {

                TileSetLoader tileSetLoader = new TileSetLoader();

                _editorTool.Level.TileSets.Add(tileSetLoader.Load(ofdOpenTileset.FileName));
                cobTileSets.SelectedIndex = cobTileSets.Items.Add((_editorTool.Level.TileSets.Count - 1).ToString() + " - " + _editorTool.Level.TileSets[_editorTool.Level.TileSets.Count - 1].Name);
            }

        }

        private int GetIndexFromSelection() {
            return Convert.ToInt32(Regex.Split(cobTileSets.Text, " - ")[0]);
        }

        private void tileSetGroupedAutoborderView1_TileSelectionChange(object sender, EventArgs e) {

            Tile selectedTile = tileSetGroupedAutoborderView1.SelectedTile;

            if (selectedTile == null) {
                return;
            }

            Rectangle rectangle = new Rectangle(selectedTile.NumberX, selectedTile.NumberY, 1, 1);

            _editorTool.ChoosenTiles = rectangle;
            tileSetRender1.Selection = rectangle;
        }

        private void tacTileSet_SelectedIndexChanged(object sender, EventArgs e) {

            switch (tacTileSet.SelectedTab.Name) {

                case "tabTileObjects":

                    cobTileObjects.Items.Clear();

                    for (int i = 0; i < _editorTool.ChoosenTileSet.TileObjectGroups.Count; i++) {
                        cobTileObjects.Items.Add( i.ToString() + " - " + _editorTool.ChoosenTileSet.TileObjectGroups[i].Name);
                    }

                    if (cobTileObjects.Items.Count > _lastSelectedTileObjectGroupIndex) {
                        cobTileObjects.SelectedIndex = _lastSelectedTileObjectGroupIndex;
                    }

                    break;
            }

        }

        private void cobTileObjects_SelectedIndexChanged(object sender, EventArgs e) {

            _lastSelectedTileObjectGroupIndex = cobTileObjects.SelectedIndex;

            TileObjectGroup tileObjectGroup = _editorTool.ChoosenTileSet.TileObjectGroups[_lastSelectedTileObjectGroupIndex];
            
            Graphics graphics = pnlSelectedObject.CreateGraphics();

            graphics.Clear(Color.White);

            for (int x = 0; x < tileObjectGroup.Rectangle.Width; x++) {
                for (int y = 0; y < tileObjectGroup.Rectangle.Height; y++) {
                    graphics.DrawImage(_editorTool.ChoosenTileSet.Tiles[tileObjectGroup.Rectangle.Left + x, tileObjectGroup.Rectangle.Top + y].Bitmap, x * TileConstants.TileSizeX, y * TileConstants.TileSizeY);
                }
            }

            _editorTool.ChoosenTileObjectGroup = tileObjectGroup;

        }

    }
}
